package com.cskaoyan.market.service.wx.impl;

import com.cskaoyan.market.db.domain.MarketCategory;
import com.cskaoyan.market.db.domain.MarketCategoryExample;
import com.cskaoyan.market.db.mapper.MarketCategoryMapper;
import com.cskaoyan.market.service.wx.WxCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 组装成复杂的vo响应体 层级嵌套关系
 * pid: 0代表的是大的标签分类 一级类目
 * 不为0 二级类目指向了某条数据的编号id
 */
@Service
public class WxCategoryServiceImpl implements WxCategoryService {

    @Autowired
    MarketCategoryMapper categoryMapper;

    @Override
    public Map<String,Object> index() {
        Map<String, Object> result = new HashMap<>();
        MarketCategoryExample categoryExample = new MarketCategoryExample();
        categoryExample.or().andDeletedEqualTo(false).andLevelEqualTo("L1");
        List<MarketCategory> categoryList = categoryMapper.selectByExample(categoryExample);
        MarketCategory currentCategory = categoryList.get(0);
        result.put("currentCategory",currentCategory);
        result.put("categoryList",categoryList);
        Integer id = currentCategory.getId();
        MarketCategoryExample categorySubExample = new MarketCategoryExample();
        categorySubExample.or().andLevelEqualTo("L2").andPidEqualTo(id);
        List<MarketCategory> currentSubCategory = categoryMapper.selectByExample(categorySubExample);
        result.put("currentSubCategory",currentSubCategory);
        return result;
    }

    @Override
    public Map<String, Object> current(Integer id) {
        Map<String,Object> result = new HashMap<>();
        MarketCategory currentCategory = categoryMapper.selectByPrimaryKey(id);
        result.put("currentCategory",currentCategory);
        MarketCategoryExample categoryExample = new MarketCategoryExample();
        categoryExample.or().andDeletedEqualTo(false).andPidEqualTo(id);
        List<MarketCategory> currentSubCategory = categoryMapper.selectByExample(categoryExample);
        result.put("currentSubCategory",currentSubCategory);
        return result;
    }
}